<?php
/**
 * @package model.dao
 * @author Sanu S <sanu@synergyquad.com>
 * @author Modified By Sanu S
 * @copyright Copyright &copy; 2011, synergyquad
 * @since 1.0 - May-18-2011
 * @version 1.1
 */

require_once ("../services/scripts.php");

class ManageAccess
{
	
	/**
	 * @access public
	 * @staticvar Instance of MemberDAOImpl
	 */
	public static $_instance	= null;
	
	/**
	 * @access public
	 */
	var $_mdb2				= null;
	
	/**
	 * <p>Get an instance of MemberDAOImpl</p>
	 * 
	 * @access public
	 * @return MemberDAOImpl Instance of MemberDAOImpl
	 */
	public static function getInstance() 
	{
		if(self::$_instance == null) 
		{
			self::$_instance = new ManageAccess();
		}
		
		return self::$_instance;
	}
		
	/**
	 * <p>To get the user start up data by the given user id</p>
	 * 
	 * @access public
	 * @global Array Configuration Array
	 * @param Integer $userId The id of the User	 
	 * @return $userStartUp the UserSettingsVO  and MemberRegVO
	 */
    public function checkAccess($role_id, $module_id)
    {
     
         $result = mysql_query("SELECT access_id FROM  privilege_master WHERE role_id=".$role_id." AND module_id=".$module_id)or die("Query ERROR");
         if($result)
             return true;
         else
             return false;
         
    }
    public function getFeatures($role_id, $module_id)
    {
        $features=null;
       // "SELECT privilege_master.module_id, module_master.module_name, access_master.access_mode FROM privilege_master INNER JOIN module_master ON privilege_master.module_id=module_master.module_id INNER JOIN access_master ON privilege_master.access_id=access_master.access_id WHERE privilege_master.role_id=".$role_id." AND module_master.parent_module_id=".$module_id;
       /// echo "SELECT privilege_master.module_id, module_master.module_name, access_master.access_mode FROM privilege_master INNER_JOIN module_master ON privilege_master.module_id=module_master.module_id INNER JOIN access_master ON privilege_master.access_id=access_master.access_id WHERE privilege.role_id=".$role_id." AND module_master.parent_module_id=".$module_id;
        $result = mysql_query("SELECT privilege_master.module_id, module_master.module_name, access_master.access_mode FROM privilege_master INNER JOIN module_master ON privilege_master.module_id=module_master.module_id INNER JOIN access_master ON privilege_master.access_id=access_master.access_id WHERE privilege_master.role_id=".$role_id." AND module_master.parent_module_id=".$module_id);
        $i=0;
        while($row = mysql_fetch_array($result))
        {
            if(!isset($features[$row["module_name"]]))
                $features[$row["module_name"]]=array();
           // $features[$row["module_name"]][$row["access_mode"]]=true;
       //     $arr = arrar    
           array_push($features[$row["module_name"]],$row["access_mode"]);
          //  $features[$i][$row["module_name"]]=$row["access_mode"];
            //$i++;
        }   
        return $features;
    }
    
    public function getVehicleRole($vehicle_id,$compnay_alias)
    { 
       $query = "SELECT role_id FROM ".$compnay_alias."_vehicle WHERE vehicle_id=".$vehicle_id;
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        return $row["role_id"];
        
    }
    
    public function getFeatureDetails($roleid) {
        $result = mysql_query("SELECT role_master.role_id,role_master.role_name,privilege_master.access_id,privilege_master.module_id,access_master.access_mode,module_master.module_name FROM role_master LEFT JOIN privilege_master ON role_master.role_id=privilege_master.role_id LEFT JOIN access_master ON privilege_master.access_id=access_master.access_id LEFT JOIN module_master ON privilege_master.module_id=module_master.module_id WHERE role_master.role_id=".$roleid)or die("Query ERROR");
        $i=0;
        $privilege=NULL;
        $modules=NULL;
        while($row = mysql_fetch_array($result))
        {
            
        //   if(array_key_exist($row["module_name"], $privilege))
             //  array_push($privilege[$row["module_name"]],$row["access_mode"]);
         //  else
               
          //  $privilege["module_id"]=$row["module_id"];
           // $privilege["module_name"]=$row["module_name"];
           
            //$privilege["access_mode"]=$row["access_mode"];
            
           // $i++;
            
            
        }
        
        return array($privilege);
    }
    public function getUserDetails($user_id)
    {
          $query_data = "SELECT user_master.*, company.company_name, company.company_alias, company.logo_url, user_roles.role_id, role_master.role_name FROM user_master LEFT JOIN company ON user_master.company_id=company.id LEFT JOIN user_roles ON user_master.user_id=user_roles.user_id LEFT JOIN role_master ON user_roles.role_id=role_master.role_id WHERE user_master.user_id = '".$user_id."'";
		return mysql_fetch_object(mysql_query($query_data));
             
    }
} 
?>